﻿using Framework;
using Framework.Entity;
using Framework.Event;
using Framework.FSM;
using Framework.ObjectPool;
using Framework.Scene;
using Framework.UI;

using Framework.Resource;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BridgeShips;
using System;
namespace BridgeShips {
    public class GameEntry : MonoBehaviour {

        #region Config

        [Tooltip( "AssetLoadAgent Count" )]
        [SerializeField]
        private int m_AssetLoadAgentCount = 0;


        #endregion
        private float rot = 0;
        private float speed = 0.25f;

        private static GameEntry m_Instance = null;
        public static GameEntry Instance {
            get {
                return m_Instance;
            }
        }
        private FrameworkEntry m_FrameworkEntry;
        private ISceneManager m_SceneManager;
        public ISceneManager SceneManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_SceneManager == null)
                    InitSceneManager();
                return m_SceneManager;
            }
        }
        private IResourceManager m_ResourceManager;
        public IResourceManager ResourceManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_ResourceManager == null)
                    InitResourceManager();
                return m_ResourceManager;
            }
        }

        private IFsmManager m_FsmManager;
        public IFsmManager FsmManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_FsmManager == null)
                    InitFsmManager();
                return m_FsmManager;
            }
        }
        private IEntityManager m_EntityManager;
        public IEntityManager EntityManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_EntityManager == null)
                    InitEntityManager();
                return m_EntityManager;
            }
        }

        //game client
        private VehicleEntityManager m_vechileEntityManager;
        public VehicleEntityManager VehicleEntityManager {
            get {
                if (m_vechileEntityManager == null) {
                    m_vechileEntityManager = VehicleEntityManager.Instance;
                    m_vechileEntityManager.InitStart();
                }
                return m_vechileEntityManager;
            }

        }

        private CombinedDeformationManager m_CombinedDeformationMgr;
        public CombinedDeformationManager CombinedDeformationManager {
            get {
                if (m_CombinedDeformationMgr == null) {
                    m_CombinedDeformationMgr = CombinedDeformationManager.Instance;
                }
                return m_CombinedDeformationMgr;
            }

        }

        //game client
        private GameSetting m_GameSetting;
        public GameSetting GameSetting {
            get {
                if (m_GameSetting == null) {
                    m_GameSetting = GameSetting.Instance;
                }
                return m_GameSetting;
            }
        }


        private IEventManager m_EventManager;
        public IEventManager EventManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_EventManager == null)
                    InitEventManager();
                return m_EventManager;
            }
        }
        private IObjectPoolManager m_PoolManager;
        public IObjectPoolManager PoolManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_PoolManager == null)
                    InitPoolManager();
                return m_PoolManager;
            }
        }

        private IUIManager m_UIManager;
        public IUIManager UIManager {
            get {
                if (m_FrameworkEntry == null)
                    m_FrameworkEntry = FrameworkEntry.Instance;
                if (m_UIManager == null)
                    InitUIManager();
                return m_UIManager;
            }
        }

        private VirtualRehearseManager m_VirtualRManager;
        public VirtualRehearseManager VirtualRManager {
            get {
                if (m_VirtualRManager == null) {
                    m_VirtualRManager = VirtualRehearseManager.Instance;
                    m_VirtualRManager.Start();
                }
                return m_VirtualRManager;
            }
        }

        

        private void Awake() {
#if UNITY_EDITOR
            Debug.Log( "Init GameFramework" );
#endif
            m_Instance = this;
            DontDestroyOnLoad( gameObject );
            m_FrameworkEntry = FrameworkEntry.Instance;
            //初始化管理器
            InitManager();
        }

        public void PostInit() {
            StartCoroutine( "LoadSceneComplete", 1.0f );
        }

        private IEnumerator LoadSceneComplete( float delayTime ) {

            yield return new WaitForSeconds( delayTime );
            VehicleEntityManager.InitStart();
        }

        private void Update() {
            if (m_FrameworkEntry == null) return;
            m_FrameworkEntry.Update( Time.deltaTime, Time.unscaledDeltaTime );
            UpdataSkyBox();
            UpDataUI();

            CombinedDeformationManager.Update();
        }
        private void UpDataUI() {
            if (Input.GetKeyDown( KeyCode.T )) {

            }
            else if (Input.GetKeyDown( KeyCode.M )) {
            }
        }
        private void UpdataSkyBox() {

            Material sky = RenderSettings.skybox;
            rot += speed * Time.deltaTime;
            rot %= 360;
            sky.SetFloat( "_Rotation", rot );

        }

        private void OnDestroy() {
            if(m_FrameworkEntry != null)
            m_FrameworkEntry.Destroy();
        }
        public void ResetGameFramework() {
            if (m_FrameworkEntry != null)
                m_FrameworkEntry.Reset();
        }

        private GameObject CreateGameObject( string name, Transform parent ) {
            GameObject go = new GameObject( name );
            go.transform.SetParent( parent );
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
            return go;
        }
        private void InitManager() {
            InitEventManager();
            // InitResourceManager();
            InitSceneManager();
            InitFsmManager();
            InitEntityManager();
            InitPoolManager();
            InitUIManager();

            StartCoroutine( ChangeScene() );
        }

        private IEnumerator ChangeScene() {
            if (m_SceneManager != null) {
                m_SceneManager.LoadScene( "startScene" );
            }
            yield return 0;
        }

        private void InitSceneManager() {
            //初始化场景模块
            m_SceneManager = m_FrameworkEntry.SceneManager;
            // UnityEngine.SceneManagement.SceneManager.LoadScene( "startScene" );
            GameObject sceneManagerGo = CreateGameObject( "SceneManager", transform );
            GameObject loadSceneAgentGo = CreateGameObject( "LoadSceneAgent", sceneManagerGo.transform );
            LoadSceneAgent loadSceneAgent = sceneManagerGo.AddComponent<LoadSceneAgent>();
            m_SceneManager.AddLoadSceneAgent( loadSceneAgent );
        }
        private void InitResourceManager() {

            //初始化资源加载模块
            m_ResourceManager = m_FrameworkEntry.ResourceManager;
            GameObject resourceManagerGo = CreateGameObject( "ResourceManager", transform );
            m_ResourceManager.AddAssetHelper( new AssetHelper() );
            for (int i = 0; i < m_AssetLoadAgentCount; i++) {
                GameObject loadAssetHelperGo = CreateGameObject( "LoadAssetHelper", resourceManagerGo.transform );
                LoadAssetHelper loadAssetHelper = loadAssetHelperGo.AddComponent<LoadAssetHelper>();
                m_ResourceManager.AddLoadAssetAgent( loadAssetHelper );
            }
        }

        private void InitFsmManager() {
            m_FsmManager = m_FrameworkEntry.FsmManager;
        }
        private void InitEntityManager() {
            m_EntityManager = m_FrameworkEntry.EntityManager;
        }

        private void InitEventManager() {
            m_EventManager = m_FrameworkEntry.EventManager;
        }
        private void InitPoolManager() {
            m_PoolManager = m_FrameworkEntry.PoolManager;
        }

        private void InitUIManager() {
            m_UIManager = m_FrameworkEntry.UIManager;
            //m_UIManager.CreateUI("StartUI",)
        }

    }
}